/**
 * Copyright 2005 SIB Framework

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package org.sibframework.utils;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


public class TextFormat {
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat stf = new SimpleDateFormat("HH:mm:ss");
	private static SimpleDateFormat sdtf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static DecimalFormat df = new DecimalFormat("0.00");
	private static NumberFormat cf = NumberFormat.getCurrencyInstance(Locale.US);
	static {
		cf.setMaximumFractionDigits(0);
	}
	private static int afterZore = 0;
	
	public static int getAfterZore() {
		return afterZore;
	}

	public static void setAfterZore(int afterZore) {
		TextFormat.afterZore = afterZore;
	}

	
	//Number
	public static String format(String str) {
		if (str == null) {
			return "";
		}
		return str;
	}

	public static String format(Integer para) {
		if (para == null || para == 0) {
			return "";
		}
		return String.valueOf(para);
	}
	

	public static String format(Long para) {
		if (para == null || para == 0) {
			return "";
		}
		return String.valueOf(para);
	}
	
	public static String format(Float flt) {
		if (flt == null || flt == 0) {
			return "";
		}
		String str = df.format(flt);
		if (afterZore > 0) {
			while (str.indexOf(".") + afterZore > str.length()) {
				str += "0";
			}
		}
		return str;
	}
	
	public static String format(Double dbl) {
		if (dbl == null || dbl == 0) {
			return "";
		}
		String str = df.format(dbl);
		if (afterZore > 0) {
			if (str.indexOf(".") < 0) {
				str += ".000";
			} else {
				while (str.indexOf(".") + afterZore >= str.length()) {
					str += "0";
				}
			}
		}
		return str;
	}

	
	//Currency
	public static String formatCurrency(Integer para) {
		if (para == null || para == 0) {
			return "";
		}
		return cf.format(para);
	}
	

	public static String formatCurrency(Long para) {
		if (para == null || para == 0) {
			return "";
		}
		return cf.format(para);
	}
	
	public static String formatCurrency(Float flt) {
		if (flt == null || flt == 0) {
			return "";
		}
		String str = cf.format(flt);
		if (afterZore > 0) {
			while (str.indexOf(".") + afterZore > str.length()) {
				str += "0";
			}
		}
		return str;
	}
	
	public static String formatCurrency(Double dbl) {
		if (dbl == null || dbl == 0) {
			return "";
		}
		String str = cf.format(dbl);
		if (afterZore > 0) {
			if (str.indexOf(".") < 0) {
				str += ".000";
			} else {
				while (str.indexOf(".") + afterZore >= str.length()) {
					str += "0";
				}
			}
		}
		return str;
	}
	public static String formatCurrency(String str) {
		if (str == null) {
			return "";
		}
		try {
			return cf.format(Double.parseDouble(str));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return str;
	}
	
	
	//date time
	public static String formatDate(Date date) {
		if (date == null) {
			return "";
		}
		return sdf.format(date);
	}
	
	public static String formatTime(Date date) {
		if (date == null) {
			return "";
		}
		return stf.format(date);
	}
	
	public static String formatDateTime(Date date) {
		if (date == null) {
			return "";
		}
		return sdtf.format(date);
	}
	
}
